package com.glsc.ngateway.opmanage.controller.task;

import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.Role;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.User;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.task.TaskInfo;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.task.TaskInfoConfig;
import com.glsc.ngateway.opmanage.aspect.UserCache;
import com.glsc.ngateway.opmanage.dto.TaskQueryDto;
import com.glsc.ngateway.opmanage.service.task.TaskInfoService;
import com.glsc.ngateway.opmanage.utils.RequestTool;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 运管待办任务
 */
@RestController
@RequestMapping("/api/task/info")
@Slf4j
public class TaskInfoController {

    @Resource
    private TaskInfoService taskInfoService;

    @Resource
    private RequestTool requestTool;

    @Resource
    private UserCache userCache;


    /**
     * 分页查询
     *
     * @param
     * @return
     */
    @PostMapping("/page")
    public PlatformResponse<Page<TaskInfo>> findClientPage(@RequestBody TaskQueryDto dto, HttpServletRequest request) {

        Map<String, Object> param = new HashMap<>();
        if (StringUtils.isNotBlank(dto.getName())) {
            param.put("LIKE_name", dto.getName());
        }
        if (StringUtils.isNotBlank(dto.getType())) {
            param.put("LIKE_type", dto.getType());
        }

        if (StringUtils.isNotBlank(dto.getQueryType()) && "2".equals(dto.getQueryType())) {
            //查询个人订阅的待办
            User user = userCache.get(request);
            param.put("EQ_userId", user.getId());
        }

        User currentUser = requestTool.getUserFromRequest(request);
        if (CollectionUtils.isNotEmpty(currentUser.getRoles())) {
            Set<String> roleCodeSet = currentUser.getRoles().stream().map(Role::getName).collect(Collectors.toSet());
            Page<TaskInfo> taskInfos = taskInfoService.listByRoleCodes(dto.getPageNo(), dto.getPageSize(), param, roleCodeSet, dto.getQueryType());
            return PlatformResponse.successData(taskInfos);
        }
        return PlatformResponse.successData(Page.empty());
    }


    /**
     * 手动点击完成待办任务
     *
     * @param id
     * @return
     */
    @PostMapping("/finishTaskInfo")
    public PlatformResponse<?> finishTaskInfo(@ApiParam(required = false, value = "id") @RequestParam(required = true) Integer id) {
        taskInfoService.finishTaskInfo(id);
        return PlatformResponse.success();
    }


    /**
     * 查询代办订阅配置
     *
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("/config/page")
    public PlatformResponse<Page<TaskInfoConfig>> findConfigPage(@RequestBody TaskQueryDto dto, HttpServletRequest request) {
        Map<String, Object> param = new HashMap<>();
        if (StringUtils.isNotBlank(dto.getName())) {
            param.put("LIKE_name", dto.getName());
        }
        if (StringUtils.isNotBlank(dto.getType())) {
            param.put("LIKE_type", dto.getType());
        }

        User user = userCache.get(request);
        param.put("EQ_userId", user.getId());

        User currentUser = requestTool.getUserFromRequest(request);
        if (CollectionUtils.isNotEmpty(currentUser.getRoles())) {
            Page<TaskInfoConfig> taskInfos = taskInfoService.listMyTaskConfigPage(dto.getPageNo(), dto.getPageSize(), param);
            return PlatformResponse.successData(taskInfos);
        }
        return PlatformResponse.successData(Page.empty());
    }

    /**
     * 保存待办订阅配置
     *
     * @param taskInfoConfig
     * @return
     */
    @PostMapping("/config/save")
    public PlatformResponse<?> saveConfig(@RequestBody TaskInfoConfig taskInfoConfig, HttpServletRequest request) {
        User user = userCache.get(request);
        taskInfoConfig.setUserId(user.getId());
        TaskInfoConfig taskInfoConfig1 = taskInfoService.saveConfig(taskInfoConfig);
        return PlatformResponse.successData(taskInfoConfig1);
    }

    /**
     * 删除待办订阅配置
     *
     * @param taskInfoConfig
     * @return
     */
    @PostMapping("/config/delete")
    public PlatformResponse<?> deleteConfig(@RequestBody TaskInfoConfig taskInfoConfig) {
        taskInfoService.deleteConfig(taskInfoConfig.getId());
        return PlatformResponse.success();
    }


}
